package my.tool;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class MybatisApi {

	public static SqlSessionFactory sqlSessionFactory;

	static {
		System.out.println("mybatis初始化------------");
		try {
			String resource = "mapper/mybatis-config.xml";
			ClassPathResource classPathResource = new ClassPathResource(resource);
			InputStream inputStream =classPathResource.getInputStream();
			
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//显示的初始化
	public static SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}
	/*
	 * 使用pojo对象操作数据库
	 * sql_url:sql语句的映射地址
	 * factor:操作条件
	 * */
	
	//插入一条数据
	public static void insert(String sql_url,Object factor) {
		try(SqlSession session = sqlSessionFactory.openSession(true)){
			session.insert(sql_url,factor);
		}
	}
	
	//删除一条数据
	public static void delete(String sql_url,Object factor) {
		try(SqlSession session = sqlSessionFactory.openSession(true)){
			session.delete(sql_url,factor);
		}
	}
	
	//修改一条数据
	public static void update(String sql_url,Object factor) {
		try(SqlSession session = sqlSessionFactory.openSession(true)){
			session.update(sql_url,factor);
		}
	}
	
	//查询一条数据返回0或1
	public static Object select(String sql_url,Object factor) {
		try(SqlSession session = sqlSessionFactory.openSession(true)){
			return session.selectOne(sql_url,factor);
		}
	}
	
	//查询多条数据
	public static List selectList(String sql_url,Object factor) {
		try(SqlSession session = sqlSessionFactory.openSession(true)){
			return session.selectList(sql_url,factor);
		}
	}
	
	//查询所有数据并自动转换为pojo对象
		public static List selectList(String sql_url,Class clazz) {
			try(SqlSession session = sqlSessionFactory.openSession(true)){
				return session.selectList(sql_url,clazz);
			}
		}
		
		//使用map
		/** 插入一个map对象
		 * 
		 * @param map 待插入的map对象
		 */
		public static void insert(String Sql_Url,Map<String, Object> map)
		{
			try(SqlSession session = sqlSessionFactory.openSession(true))
			{
				session.insert(Sql_Url, map);
				
			}
		}
		
		/** 删除一个map对象
		 * 
		 * @param map 待删除的map对象
		 */
		public static void delete(String Sql_Url,Map<String, Object> map)
		{
			try(SqlSession session = sqlSessionFactory.openSession(true))
			{
				session.delete(Sql_Url, map);
				
			}
		}
		
		/** 修改一个map对象
		 * 
		 * @param map 待修改的map对象
		 */
		public static void update(String Sql_Url,Map<String, Object> map)
		{
			try(SqlSession session = sqlSessionFactory.openSession(true))
			{
				session.update(Sql_Url, map);
				
			}
		}
		
		/** 查询，并自动转换为map对象
		 * 
		 * @param Sql_Url 映射sql语句id地址
		 * @param 
		 */
		public static List SelectList(String Sql_Url,Map<String, Object> map)
		{
			try(SqlSession session = sqlSessionFactory.openSession(true))
			{
				return session.selectList(Sql_Url,map);
			}
		}
		
		

}
